//package com.yili.oa.entity;
//
//import com.yili.oa.models.Admin;
//import com.yili.oa.service.AdminService;
//import com.yili.oa.service.LoginService;
//import com.yili.oa.utils.Exception.AjaxException;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authc.*;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.subject.PrincipalCollection;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import java.util.Arrays;
//import java.util.List;
//
///**
// * realm实现类,用于实现具体的验证和授权方法
// * @author Bean
// *
// */
//public class MyShiroRealm extends AuthorizingRealm {
//
//    @Autowired
//    private LoginService loginService;
//
//    @Autowired
//    private AdminService adminService;
//
//    @Autowired
//    private RoleService roleService;
//
//    @Autowired
//    private PermisService permisService;
//
//    /**
//     * 方面用于认证 加密 参数：AuthenticationToken是从表单穿过来封装好的对象
//     */
//    @Override
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//        System.out.println("doGetAuthenticationInfo:" + token);
//
//        // 将AuthenticationToken强转为AuthenticationToken对象
//        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
//
//        // 获得从表单传过来的用户名
//        String username = upToken.getUsername();
//
//        // 如果用户不存在，抛此异常
//        Admin admin = loginService.getByAccount(username);
//        if (admin.getState().equals(1)){
//            throw new AjaxException("该用户已被禁用 请联系管理员");
//        }
//        if (admin==null) {
//            throw new UnknownAccountException("无此用户名！");
//        }else{
//            //放到session
//            SecurityUtils.getSubject().getSession().setAttribute("admin",admin);
//            return new SimpleAuthenticationInfo(username,admin.getPassword(),super.getName());
//        }
//
//
//    }
//
//    // 用于授权
//    @Override
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//
//        Admin user = adminService.getByAccount(principals.getPrimaryPrincipal().toString());
//        SimpleAuthorizationInfo sai  = new SimpleAuthorizationInfo();
//        //根据角色分配 权限之后再完善
//        if (user.getUserrole()!=null){
//            List<String> roleIds = Arrays.asList(user.getUserrole().split(","));
//            for (String roleId:
//                 roleIds) {
//                Role role = roleService.getById(roleId);
//                if (role==null){
//                    return sai;
//                }
//                List<String> permisId = Arrays.asList(role.getRolePermission().split(","));
//                for (String id:
//                     permisId) {
//                    Permission permission = permisService.getById(id);
//                    sai.addStringPermission(permission.getPermissionKey());
//                }
//            }
//        }
//        return sai;
//
//    }
//
//}
